import os
import time
from concurrent.futures import ProcessPoolExecutor
from threading import current_thread


def task(name):
    print('%s is running' % name)
    time.sleep(3)
    print('%s is done' % name)


def task2(name, n=0):
    print('%s is running' % name)
    time.sleep(n)
    print('%s is done' % name)


def my_function(arg1, arg2):
    result = arg1 + arg2
    time.sleep(3)
    print(f"pid: {os.getpid()}, thread: {current_thread().name}, {result}")
    return result


def format_csv(source_path, target_path):
    print(f"Processing {source_path} start")
    # with open(source_path, encoding='GBK', newline='') as csvfile:
    #     reader = csv.reader(csvfile)
    #     next(reader, None)  # 跳过标题行
    #     with open(target_path, 'w', encoding='GBK', newline='') as newfile:
    #         writer = csv.writer(newfile)
    #         for row in reader:
    #             writer.writerow([f"'{row[1].lstrip()}'", transform_value(row[2])])
    time.sleep(3)
    print(f"Processed {target_path} finish\n\n")


def process_files_in_directory(source_dir, target_dir, workers=os.cpu_count()):
    with ProcessPoolExecutor(max_workers=workers) as executor:
        for filename in os.listdir(source_dir):
            if filename.endswith(".csv"):
                source_path = os.path.join(source_dir, filename)
                target_path = os.path.join(target_dir, filename.replace('.', '_').replace('-', '_'), '.csv')
                if not os.path.exists(target_path):
                    executor.submit(format_csv, source_path, target_path)
            else:
                print('ignore file ', filename)
    executor.shutdown()


# # 开启子进程的操作必须放到
# # if __name__ == '__main__'的子代码中
# # 子进程不会再次加载
# if __name__ == '__main__':
#     # args
#     # p = Process(target=task,args=('大海',))
#     # kwargs
#     p = Process(target=task, kwargs={'name': '大海'})
#
#     # print(p)
#     # 主进程只是向操作系统发送了一个开启子进程的信号
#     p.start()
#     # 1.操作系统先申请内存空间
#     # 2.把主进程的数据拷贝到子进程里面
#     # 3.调用cup才能运行里面的代码
#     # 创造进程的开销大
#     print('主')
if __name__ == '__main__':
    # 串行
    # task('子1')
    # task('子2')
    # # 主进程只是向操作系统发送了一个开启子进程的信号
    # p1 = Process(target=task2, args=('大海1',))
    # p2 = Process(target=task2, args=('大海2',))
    # p3 = Process(target=task2, args=('大海3',))
    # p1.start()
    # p2.start()
    # p3.start()
    # # time.sleep(4)
    # # join让主进程等待子进程运行完
    # p1.join()
    # p2.join()
    # p3.join()
    # print('主')
    # 开启多个进程
    start = time.time()
    # p_l = []
    # for i in range(1, 5):
    #     p = Process(target=task2, args=('大海%s' % i, i))
    #     p_l.append(p)
    #     p.start()
    # # 主进程等待子进程
    # for p in p_l:
    #     p.join()
    # print('主', (time.time() - start))

    # 创建一个包含3个进程的进程池
    # with ProcessPoolExecutor(max_workers=4) as executor:
    #     # 提交任务给进程池，并传递多个参数
    #     for i in range(1, 100):
    #         executor.submit(my_function, i, i)
    #     executor.shutdown()

    process_files_in_directory('D:\\Workspace\\数据导入\\转换脚本', '', 3)

    print('主', (time.time() - start))
